package com.jy.controller;

        import com.jy.domain.User;
        import com.jy.service.UserService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.ResponseBody;
        import org.springframework.web.servlet.ModelAndView;

        import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
//    管理员登陆
    @RequestMapping("/loginpage")
    public ModelAndView login(User u, HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.login(u);

        if(user != null){
            modelAndView.setViewName("redirect:index");
            httpSession.setAttribute("user",user);
        }else {
            String errorInfo = "用户名密码错误";
            modelAndView.setViewName("login");
            modelAndView.addObject("loginError",errorInfo);
        }
        return modelAndView;
    }

//    查询旧密码
    @RequestMapping("/findPassword")
    public @ResponseBody String findPassword(String oldpwd,int id){
//        System.out.println(oldpwd);
//        System.out.println(id);
        User user = new User();
        user.setPassword(oldpwd);
        user.setId(id);
        if(userService.findOne(user) != null){
            return "true";
        }else{
            return "false";
        }

    }

//    修改密码
    @RequestMapping("/updatePassword")
    public @ResponseBody String updatePassword(User user){
        if(userService.updateUser(user) > 0){
            return "true";
        }else {
            return "false";
        }
    }

//    注销用户
    @RequestMapping("/logout")
    public String logout(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return "redirect:login";
}





    @RequestMapping("/{page}")
    public String toPage(@PathVariable()String page){
        return page;
    }
}
